
component  extends="ModelGlue.gesture.controller.Controller" output="false" beans="core"
{
	public ModelGlue.gesture.controller.Controller function init( required Any modelGlue){
		
		super.init( arguments.modelGlue );
		
		return this;
	}
	
	public ModelGlue.Core.Event function new( required Any event ){
		
		var service = beans.core.getService( 'Comment' );
		var Comment = service.new();
		
		Comment.setIdComment( 0 );
		Comment.setStatus( 0 );
		
		//arguments.event.makeEventBean( Comment );
		
		arguments.event.setValue( 'Comment', Comment );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function save( required Any event ){
		
		var service = beans.core.getService( 'Comment' );
		var Comment = service.new();
		var validator = service.getValidator();
		var login = beans.core.getService( 'Security' ).getLogin();
		
		Comment.setStatus( 0 );
		Comment.setIdComment( 0 );
		Comment.setUuid( createUUID() );
		Comment.setPostDate( now() );
		Comment.setName( login.getUser().getName() );
		Comment.setEmail( login.getUser().getEmail() );
		
		arguments.event.makeEventBean( Comment );
		
		arguments.event.setValue( 'Comment', Comment );
		
		validator.validate( Comment );
			
		if( validator.hasErrors() ){
		
			arguments.event.setValue( 'error', validator.getErrors() );
			
			arguments.event.setValue( 'errorBox', 'Comment not saved.' );

			arguments.event.addResult( 'save.failure' );
		
		}else{
			
				service.saveComment( Comment );
				
				arguments.event.setValue( 'successBox', 'Comment saved.' );
				
				arguments.event.addResult( 'save.success' );
			
		}
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function delete( required Any event ){
		
		var service = beans.core.getService( 'Comment' );
		var Comment = service.new();
		var val = "";
		
		arguments.event.makeEventBean( Comment );
		
		arguments.event.setValue( 'Comment', Comment );
		
		service.deleteComment( Comment );
		
		arguments.event.setValue( 'successBox', 'Comment deleted.' );
									
		arguments.event.addResult( 'save.success' );
		
		return arguments.event;
	}	
	
	
	public ModelGlue.Core.Event function get( required Any event ){
		
		var service = beans.core.getService( 'Comment' );
		var Comment = service.new();
		var result = "";
		
		arguments.event.makeEventBean( Comment );
		
		result = service.getComment( Comment );
									
		arguments.event.setValue( 'Comment', result );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function list( required Any event ){
		
		var service = beans.core.getService( 'Comment' );
		var Comment = service.new();
		var result = "";
		
		result = service.getComments( Comment );
									
		arguments.event.setValue( 'Comment.list', result );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function listComments( required Any event ){
		
		var service = beans.core.getService( 'Comment' );
		var Comment = service.new();
		var result = "";
		
		// get all comment active by post
		Comment.setStatus( 1 );
		Comment.setIdPost( arguments.event.getValue( 'idPost' ) );
		
		result = service.getComments( Comment );
									
		arguments.event.setValue( 'Comment.list', result );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function getLast( required Any event ){
		
		var service = beans.core.getService( 'Comment' );
		var Comment = service.new();
		var result = "";
		
		// get all comment active by post
		Comment.setStatus( 1 );
		
		result = service.getCommentsPaged( Comment, 1, arguments.event.getArgument( 'maxrow', 10 ) );
									
		arguments.event.setValue( 'Comment.last', result );
		
		return arguments.event;
	}
	
}
